package com.zx.deploy.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * json转换工具，基于Jackson
 */
public class JsonUtil {

    /**
     * 将对象转换成son字符串
     * @param obj
     * @return
     */
    public static String jsonEncode(Object obj){
        try {
            return  new ObjectMapper().writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 将json字符串转换化成对象
     * @param str
     * @param clz
     * @param <T>
     * @return
     */
    public static <T> T jsonDecode(String str,Class<T> clz){
        try {
            return new ObjectMapper().readValue(str,clz);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
